import { createRouter, createWebHistory } from "vue-router";
import { GetToken } from "../utils/Token"
const routes = [
  {
    path: "/",
    name: "home",
    component: () => import("../view/index.vue"),
  },
  // {
  //   path: "/",
  //   name: "home",
  //   component: () => import("../view/one.vue"),
  // },
  {
    path: "/quit",
    name: "/quit",
    component: () => import("../view/quit.vue"),
  },
  {
    path: "/GameIndex",
    name: "GameIndex",
    component: () => import("../view/Game/index.vue"),
  },
  {
    path: "/GameMap",
    name: "GameMap",
    component: () => import("../view/Game/map.vue"),
  },
  {
    path: "/GamSurvive",
    name: "GamSurvive",
    component: () => import("../view/Game/survive.vue"),
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});
router.beforeEach((to, from, next) => {

  // 如果token存在
  if (GetToken()) {
    // 当token存在判断当时是否前往登录页面
    // if (!from.name && to.name == "home") {
    //   next();
    // } else if (!from.name && to.name != "home") {
    //   next({ name: "home" });
    // } else {
    //   next();
    // } 
    next();

  } else {
    // 当token不存在的时候

    if (from.name == "home" && to.name == "/quit") {
      next();
    } else if (to.name != "home") {
      next({ name: "home" });
    } else {
      next()
    }
  }

});
export default router;
